﻿/////////////////////////////////////////////////////////////////////////
// 
// Copyright (C) 2007 e-Channels CORPORATION
// 
// ALL RIGHTS RESERVED BY e-Channels CORPORATION, THIS PROGRAM
// MUST BE USED SOLELY FOR THE PURPOSE FOR WHICH IT WAS  
// FURNISHED BY e-Channels CORPORATION, NO PART OF THIS PROGRAM
// MAY BE REPRODUCED OR DISCLOSED TO OTHERS, IN ANY FORM
// WITHOUT THE PRIOR WRITTEN PERMISSION OF e-Channels CORPORATION.
// USE OF COPYRIGHT NOTICE DOES NOT EVIDENCE PUBLICATION
// OF THE PROGRAM
// 
//			e-Channels CONFIDENTIAL AND PROPRIETARY
// 
////////////////////////////////////////////////////////////////////////////
//
// 翻页公用函数
// @version 1.3
// @author zhanghao@yuchengtech.com
//////////////////////////////////////////////////////////////////////////// 

/**
*  生成的html中包含的样式名称
*/
var TURN_PAGE_CLASS_LAST_PAGE = 'last_page' ;
var TURN_PAGE_CLASS_PAGE_SELECTED = 'page_selected' ;
var TURN_PAGE_CLASS_PAGE_NORMAL = 'page_normal' ;
var TURN_PAGE_CLASS_NEXT_PAGE = 'next_page' ;
var TURN_PAGE_CLASS_GOTO_PAGE = 'goto_page' ;
var TURN_PAGE_INPUT_DEFAULT_ID = 'turnPageInputText' ;

/**
* 查询失败
*/
function showError()
{
	alert('通讯失败，请重试');
}

/**
* 翻页栏中点击跳转时调用的函数，如果输入的页数合法，则生成那页的起始位置并调用turnPage 函数
* @return {String} html字符串
*/
function turnPageJump( lastPage,showNum,inputId )
{
	var jumpTo = document.getElementById(inputId);
	if( isInteger( jumpTo.value ) )
	{
		var page = parseInt(jumpTo.value);
		if ( page <= lastPage )
		{
			var beginPos = ( page - 1 ) * showNum + 1;
			turnPage( beginPos );
		}else{
			jumpTo.value = '';
		}
	}else{
		jumpTo.value = '';
	}
}
/**
* 生成翻页的html字符串
* @param {int} 翻页开始位置
* @param {int} 每页显示数量
* @param {int} 总记录条数
* @return {String} html字符串
*/
function getTurnPageString(turnPageBeginPos,turnPageShowNum,turnPageTotalNum)
{	
	turnPageShowNum = 10;
	if ( turnPageTotalNum == 0 || isNaN(turnPageTotalNum) )
		return "";
	<!-- 总页数 --> 
	var turnPageTotalPage;
	if ( turnPageTotalNum % turnPageShowNum > 0 )
	{
		turnPageTotalPage = parseInt( turnPageTotalNum / turnPageShowNum) + 1;
	}else{
		turnPageTotalPage = parseInt( turnPageTotalNum / turnPageShowNum);
	}
	<!-- 当前页(从1开始) -->
	var turnPageCurrentPage = parseInt( turnPageBeginPos / turnPageShowNum ) + 1;
	<!-- 是否可以翻到上一页-->
	var turnPageHasLastButton = turnPageCurrentPage > 1;
	<!-- 是否可以翻到下一页-->
	var turnPageHasNextButton = turnPageCurrentPage < turnPageTotalPage;
	<!-- 页面列出可点击的页数-->
	var turnPageListPageNum = 10;
	<!-- 当前显示可供点击的开始页数 -->
	var turnPageStart = parseInt( turnPageCurrentPage / turnPageListPageNum ) * turnPageListPageNum;
	turnPageStart = ( turnPageStart == 0 ) ? 1: turnPageStart;
	<!-- 当前显示可供点击的结束页数 -->
	var turnPageEnd = turnPageStart + turnPageListPageNum - 1 ;
	if ( turnPageEnd > turnPageTotalPage )
	{
		turnPageEnd = turnPageTotalPage;
	}
	<!-- 生成列表 -->
	var htmlString = '';
	var functionString ;
	if ( turnPageHasLastButton )
	{
		var tempBeginPos = turnPageBeginPos -  turnPageShowNum;
		functionString = 'turnPage('+tempBeginPos+')';
		htmlString += '<span onclick="'+functionString+'" class="'+TURN_PAGE_CLASS_LAST_PAGE+'">&nbsp;</span>';
	}
	for ( var i=turnPageStart; i <= turnPageEnd; i++ )
	{
		if ( i == turnPageCurrentPage )
		{
			htmlString += '<span class="'+TURN_PAGE_CLASS_PAGE_SELECTED+'"> '+i+' </span>';
		}else{
			var tempBeginPos = ( i - 1 ) * turnPageShowNum + 1;
			functionString = 'turnPage('+tempBeginPos+')';		
			htmlString += '<span onclick="'+functionString+'" class="'+TURN_PAGE_CLASS_PAGE_NORMAL+'"> '+i+' </span>';
		}
	}
	if ( turnPageHasNextButton )
	{
		var tempBeginPos = turnPageBeginPos +  turnPageShowNum;
		functionString = 'turnPage( '+tempBeginPos+')';
		htmlString += '<span onclick="'+functionString+'" class="'+TURN_PAGE_CLASS_NEXT_PAGE+'">&nbsp;</span>';
	}
	var inputId = getTurnPageInputId();
	htmlString += " 第"+turnPageCurrentPage+"页/共"+turnPageTotalPage+"页";
	htmlString += " 总计"+turnPageTotalNum+"笔";
	htmlString += " 转到第<input id='"+inputId+"' type='text' size='2' maxlength='5'/>页";
	functionString ="turnPageJump("+turnPageTotalPage+","+turnPageShowNum+",'"+inputId+"')";
	htmlString += '<span onclick="'+functionString+'" class="'+TURN_PAGE_CLASS_GOTO_PAGE+'">&nbsp;</span>';
	return htmlString;
}

/**
* 生成翻页输入框的Id
* @return {String} 输入框的Id
*/
function getTurnPageInputId()
{
	var result = TURN_PAGE_INPUT_DEFAULT_ID;
	var testInput = document.getElementById(result);
	if ( testInput )
	{
		for(var i=1; i<10; i++)
		{
			result = TURN_PAGE_INPUT_DEFAULT_ID + i ;
			testInput = document.getElementById(result);
			if ( testInput==null )
				return result;
		}	
	}
	return result;
}